package com.tech.thread.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

/**
 * @Description:
 * @Author: zl
 * @Date: 2019/7/12 15:24
 */
public class CountDownLatchTest {


    //1、CountDownLatch的构造函数接收一个int类型的参数作为计数器，如果你想等待N个点完成，这里就传入N。
    static CountDownLatch c = new CountDownLatch(2);
    public static void main(String[] args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(1);
                //2、当我们调用CountDownLatch的countDown方法时，N就会减1。
                c.countDown();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(2);
                c.countDown();
            }
        }).start();
        //3、CountDownLatch的await方法会阻塞当前线程，直到N变成零
        c.await();
        System.out.println("3");
    }
}
